﻿#include "MainWindow.h"
#include <QGridLayout>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QMessageBox>
#include <bmQT/GraphicsWindowQt.h>
#include <osgDB/ReadFile>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/TrackballManipulator>
#include <QMenu>
#include <QMenuBar>
#include <QLabel>
#include <QPushButton>
#include <bmCore/ViewerManager.h>

MainWindow::MainWindow( const std::string &windowName )
  : QMainWindow( NULL )
{

  QVBoxLayout *mainLayout = new QVBoxLayout( /*this*/ );
  // address
  QHBoxLayout *addressLayout = new QHBoxLayout( /*this*/ );
  QWidget *addressWidget = new QWidget(this);
  mainLayout->addWidget( addressWidget );
  addressWidget->setLayout( addressLayout );
  addressLayout->addWidget( new QLabel(tr("address")));
  _addressBox = new QComboBox(this);
  _addressBox->setEditable(true);
  addressLayout->addWidget(_addressBox, true );
  QPushButton *button = new QPushButton(tr("goto"));
  addressLayout->addWidget(button);

  // osg window
  QWidget *centerView = CreateOSGView( new osgViewer::Viewer, 10, 10, 800, 600 );
  mainLayout->addWidget( centerView, true );

  QWidget *centralWidget = new QWidget;
  centralWidget->setLayout( mainLayout );

  setCentralWidget( centralWidget );

  // use this timer to update scene
  connect(&_timer, SIGNAL(timeout()), this, SLOT(update()));
// 
   _timer.start(10);

}

MainWindow::~MainWindow()
{
}


QWidget *MainWindow::CreateOSGView( osgViewer::Viewer *v, int x, int y, int w, int h )
{

  // 目前只能使用单线程模式
  v->setThreadingModel(osgViewer::ViewerBase::SingleThreaded);

  v->setKeyEventSetsDone(0);

  osg::DisplaySettings* ds = osg::DisplaySettings::instance().get();
  osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
  traits->windowName = "osg";
  traits->windowDecoration = false;//windowDecoration;
  traits->x = x;
  traits->y = y;
  traits->width = w;
  traits->height = h;
  traits->doubleBuffer = true;//centerView
  traits->alpha = ds->getMinimumNumAlphaBits();
  traits->stencil = ds->getMinimumNumStencilBits();
  traits->sampleBuffers = ds->getMultiSamples();
  traits->samples = ds->getNumMultiSamples();

  bmQT::GraphicsWindowQt *gw = new bmQT::GraphicsWindowQt(traits.get());

  osg::Camera* camera = v->getCamera();
  camera->setGraphicsContext( gw );

  camera->setClearColor( osg::Vec4(0.2, 0.2, 0.6, 1.0) );
  camera->setViewport( new osg::Viewport(0, 0, traits->width, traits->height) );
  camera->setProjectionMatrixAsPerspective(30.0f, static_cast<double>(traits->width)/static_cast<double>(traits->height), 1.0f, 10000.0f );

  v->setSceneData( osgDB::readNodeFile("cow.osg" ) );
  v->addEventHandler( new osgViewer::StatsHandler );
  //_viewer->addEventHandler( new osgViewer::StatsHandler );
  v->setCameraManipulator( new osgGA::TrackballManipulator );

  bmCore::ViewerManager::Instance()->SetViewer(v);
  return gw->getGLWidget();
}


void MainWindow::paintEvent( QPaintEvent *event )
{
   if ( !bmCore::ViewerManager::Instance()->GetViewer()->done() )
   {
     bmCore::ViewerManager::Instance()->PreFrame();
     bmCore::ViewerManager::Instance()->Frame();
     bmCore::ViewerManager::Instance()->PostFrame();
   }
}
